package com.lc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lc.exception.BusinessException;
import com.lc.mapper.*;
import com.lc.pojo.*;
import com.lc.pojo.dto.*;
import com.lc.result.ResultEnum;
import com.lc.service.PmsSkuService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PmsSkuServiceImpl extends ServiceImpl<PmsSkuMapper,PmsSku> implements PmsSkuService {

    @Resource
    private PmsSpuMapper pmsSpuMapper;

    @Resource
    private PPDPMapper ppdpMapper;

    @Resource
    private PPCMapper ppcMapper;

    @Resource
    private PmsSkuMapper pmsSkuMapper;

    @Resource
    private PmsSkuPicMapper pmsSkuPicMapper;

    @Override
    public PmsSpuSkuPicDto getPmsSkuDetail(Integer spuId) {
        PmsSpuSkuPicDto pmsSpuSkuPicDto = new PmsSpuSkuPicDto();

        //商品详情
        LambdaQueryWrapper<PmsSpu> wrapperPmsSpu = new LambdaQueryWrapper<>();
        wrapperPmsSpu.eq(PmsSpu::getId, spuId);
        PmsSpu pmsSpu = pmsSpuMapper.selectOne(wrapperPmsSpu);
        if(pmsSpu == null){
            throw new BusinessException(ResultEnum.COMMON_EXCEPTION);
        }
        PmsSpuSkuDto pmsSpuSkuDto = new PmsSpuSkuDto();
        BeanUtils.copyProperties(pmsSpu, pmsSpuSkuDto);

        //商品详情图片
        LambdaQueryWrapper<PmsProductDetailPic> wrapperPPDP = new LambdaQueryWrapper<>();
        wrapperPPDP.select(PmsProductDetailPic::getId,PmsProductDetailPic::getPicUrl)
                .eq(PmsProductDetailPic::getSpuId,spuId);
        List<PmsProductDetailPic> listPPDP = ppdpMapper.selectList(wrapperPPDP);
        List<PPDPDto> listPPDPDto = new ArrayList<>();
        listPPDP.forEach(ppdp->{
            PPDPDto ppdpDto = new PPDPDto();
            BeanUtils.copyProperties(ppdp,ppdpDto);
            listPPDPDto.add(ppdpDto);
        });
        pmsSpuSkuDto.setProductDetailPicList(listPPDPDto);
        pmsSpuSkuPicDto.setSpu(pmsSpuSkuDto);

        //商品分类数组
        LambdaQueryWrapper<PmsProductCategory> wrapperPPC = new LambdaQueryWrapper<>();
        wrapperPPC.in(PmsProductCategory::getId,pmsSpu.getCid1(),pmsSpu.getCid2());
        List<PmsProductCategory> listPPC = ppcMapper.selectList(wrapperPPC);
        ArrayList<PPCSSPDto> listPPCSSPDto = new ArrayList<>();
        listPPC.forEach(ppc->{
            PPCSSPDto ppcsspDto = new PPCSSPDto();
            BeanUtils.copyProperties(ppc,ppcsspDto);
            listPPCSSPDto.add(ppcsspDto);
        });
        pmsSpuSkuPicDto.setCategories(listPPCSSPDto);

        //商品列表
        LambdaQueryWrapper<PmsSku> wrapperPmsSku = new LambdaQueryWrapper<>();
        wrapperPmsSku.eq(PmsSku::getSpuId,spuId);
        List<PmsSku> listPmsSku = pmsSkuMapper.selectList(wrapperPmsSku);
        //商品列表中对应的图片信息
        LambdaQueryWrapper<PmsSkuPic> wrapperPmsSkuPic = new LambdaQueryWrapper<>();
        wrapperPmsSkuPic.in(PmsSkuPic::getSkuId,listPmsSku);
        List<PmsSkuPic> listPmsSkuPic = pmsSkuPicMapper.selectList(wrapperPmsSkuPic);
        List<PmsSkuPicDto> listPmsSkuPicDto = new ArrayList<>();
        listPmsSkuPic.forEach(psp->{
            PmsSkuPicDto pmsSkuPicDto = new PmsSkuPicDto();
            BeanUtils.copyProperties(psp,pmsSkuPicDto);
            listPmsSkuPicDto.add(pmsSkuPicDto);
        });

        //商品sku列表集
        List<PmsSkuDto> listPmsSkuDto = new ArrayList<>();
        listPmsSku.forEach(ps->{
            PmsSkuDto pmsSkuDto = new PmsSkuDto();
            pmsSkuDto.setSkuPicList(listPmsSkuPicDto);
            BeanUtils.copyProperties(ps,pmsSkuDto);
            listPmsSkuDto.add(pmsSkuDto);
        });
        pmsSpuSkuPicDto.setSkuList(listPmsSkuDto);

        return pmsSpuSkuPicDto;
    }
}
